home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 111_01 / count.c < prev    next >
Text File  |  1985-08-19  |  5KB  |  194 lines

  1. /*
  2. HEADER:        ;
  3. TITLE:        Count;
  4. VERSION:    1.1;
  5.  
  6. DESCRIPTION:    "Counts the lines, words, or characters in a file.";
  7.  
  8. KEYWORDS:    File, utility, count;
  9. SYSTEM:        CP/M-80;
  10. FILENAME:    COUNT.C;
  11. AUTHORS:    Unknown;
  12. COMPILERS:    BDS C;
  13. */
  14. /************************************************************************
  15.  
  16.     Ver. 1.0:
  17.         Author and date unknown.
  18.     Ver. 1.1:
  19.         Updated to BDS C, v1.44
  20.         Rick Hollinbeck, Norwood, CO 81423
  21.  
  22. *************************************************************************
  23.     : : : : : : : : : : : : : : : : : : : : : : : : : : : :
  24.  
  25.         Macros for constant definitions
  26.  
  27.     : : : : : : : : : : : : : : : : : : : : : : : : : : : :
  28. */
  29. #include <bdscio.h>    /* For BDS C, V1.4 */
  30.  
  31. #define EOFF -1        /* end of file marker returned by getc() */
  32. #define NOFILE -1    /* no such file indication given by fopen() */
  33.  
  34. #define YES 1        /* true */
  35. #define NO 0        /* false */
  36.  
  37. #define EOF 0x1A    /* CP/M's end file char for ascii files */
  38. #define CR 0x0D        /* Carriage return */
  39.  
  40. /*    -------------------------------------------------------
  41.  
  42.     Name:        main(argc,argv)
  43.     Result:        ---
  44.     Errors:        invocation syntax or no such file
  45.     Globals:    ---
  46.     Macros:        NOFILE
  47.     Procedures:    badcmd(),printf(),chrcount(),wrdcount()
  48.             lincount(),exit(),fopen(),tolower()
  49.  
  50.     Action:        Interpert the command line
  51.             handle invocation errors
  52.             open file for buffered input
  53.             handle no file error
  54.             call the counting routine specified
  55.             print results of counting routine on console
  56.  
  57.     ------------------------------------------------------- */
  58.  
  59.  
  60. main(argc,argv)
  61.     int argc;
  62.     char *argv[];
  63.     {
  64.     int fd;
  65.     char inbuf[BUFSIZ];
  66.  
  67.     if(argc != 3)
  68.       badcmd();
  69.     else if( (fd = fopen(argv[1],inbuf)) == NOFILE )
  70.       printf("No such file %s\n",argv[1]);
  71.     else if( tolower( *argv[2] ) == 'c' )
  72.       printf("There are %u characters in file %s\n",
  73.             chrcount(inbuf),argv[1]);
  74.     else if( tolower( *argv[2] ) == 'w' )
  75.       printf("There are %u words in file %s\n",wrdcount(inbuf),argv[1]);
  76.     else if( tolower( *argv[2] ) == 'l' )
  77.       printf("There are %u lines in file %s\n",lincount(inbuf),argv[1]);
  78.     else
  79.       badcmd();
  80.     exit();
  81.     }
  82.  
  83. /*    -------------------------------------------------------
  84.  
  85.     Name:        badcmd()
  86.     Result:        ---
  87.     Errors:        ---
  88.     Globals:    ---
  89.     Macros:        ---
  90.     Procedures:    puts()
  91.  
  92.     Action:        Print the invocation error message
  93.             on the console
  94.  
  95.     ------------------------------------------------------- */
  96.  
  97.  
  98. badcmd()
  99.     {
  100.     puts("Correct invocation form is: COUNT <filename> <item>\n");
  101.     puts("Where <item> is C[har] or W[ords] or L[ines]\n");
  102.     return;
  103.     }
  104.  
  105. /*    : : : : : : : : : : : : : : : : : : : : : : : : : : : :
  106.     function chrcount --count the characters in the specified input file
  107.     : : : : : : : : : : : : : : : : : : : : : : : : : : : : */
  108. /*    -------------------------------------------------------
  109.  
  110.     Name:        chrcount(file)
  111.     Result:        # of chars in file
  112.     Errors:        ---
  113.     Globals:    ---
  114.     Macros:        EOFF,EOF
  115.     Procedures:    getc()
  116.  
  117.     Action:        ---
  118.  
  119.     ------------------------------------------------------- */
  120.  
  121.  
  122. chrcount(file)
  123.     char file[];    /* the file buffer */
  124.     {
  125.     unsigned cc;    /* character count */
  126.     int c;        /* 1 char buffer */
  127.  
  128.     cc =0;
  129.     while( (c = getc(file)) != EOFF && c != EOF)
  130.       cc++;
  131.     return cc;
  132.     }        /* end chrcount */
  133.  
  134.  
  135. /*    -------------------------------------------------------
  136.  
  137.     Name:        wrdcount(file)
  138.     Result:        # of words (strings enclosed in space) in file
  139.     Errors:        ---
  140.     Globals:    ---
  141.     Macros:        EOFF,EOF,CR,YES,NO
  142.     Procedures:    getc(),isspace()
  143.  
  144.     Action:        ---
  145.  
  146.     ------------------------------------------------------- */
  147.  
  148. wrdcount(file)
  149.     char file[];    /* the file buffer */
  150.     {
  151.     int inword;    /* switch to tell if the present char is in a word */
  152.     unsigned wc;    /* word count */
  153.     int c;        /* 1 char buffer */
  154.  
  155.     wc =0;
  156.     inword = NO;
  157.     while( (c = getc(file)) != EOFF && c != EOF)
  158.       if(isspace(c) || c == CR)
  159.         inword = NO;
  160.       else if(inword == NO) {
  161.         inword = YES;
  162.         wc++;
  163.         }
  164.     return wc;
  165.     }        /* end wrdcount */
  166.  
  167.  
  168. /*    -------------------------------------------------------
  169.  
  170.     Name:        lincount(file)
  171.     Result:        # of lines ('\n's) in file
  172.     Errors:        ---
  173.     Globals:    ---
  174.     Macros:        EOFF,EOF
  175.     Procedures:    getc()
  176.  
  177.     Action:        ---
  178.  
  179.     ------------------------------------------------------- */
  180.  
  181.  
  182. lincount(file)
  183.     char file[];    /* the file buffer */
  184.     {
  185.     unsigned lc;    /* line count */
  186.     int c;        /* 1 char buffer */
  187.  
  188.     lc =0;
  189.     while( (c = getc(file)) != EOFF && c != EOF)
  190.       if(c == '\n')
  191.         lc++;
  192.     return lc;
  193.     }        /* end lincount */
  194.